package top.ivansong.zeus.auth.center.storage.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import top.ivansong.zeus.auth.center.constant.AuthenticateType;
import top.ivansong.zeus.auth.center.storage.entry.UserAuthentication;
import top.ivansong.zeus.auth.center.storage.entry.UserAuthenticationPassword;
import top.ivansong.zeus.auth.center.storage.mapper.UserAuthenticationMapper;

public class UserDao {
    @Autowired
    private UserAuthenticationMapper userAuthenticationMapper;

    @Cacheable(value = "UserDao.loadAuthentication", key = "#type.name():#content")
    public UserAuthentication loadAuthentication(AuthenticateType type, String content) {
        return userAuthenticationMapper.loadAuthenticationByTypeAndContent(type, content);
    }

    @Cacheable(value = "UserDao.loadPassword")
    public UserAuthenticationPassword loadPassword(Long userId) {
        return userAuthenticationMapper.loadPasswordByUserId(userId);
    }

    @CacheEvict(value = "UserDao.loadAuthentication", key = "#type.name():#newContent")
    public void insertAuthentication(Long userId, AuthenticateType type, String newContent, String oldContent) {
        userAuthenticationMapper.insertOrUpdateAuthenticationByUserIdAndType(authentication);
    }
}
